package com.atguigu.campusintelligentassistantsystem.controller;

import com.atguigu.campusintelligentassistantsystem.entity.Course;
import com.atguigu.campusintelligentassistantsystem.entity.StudentCourse;
import com.atguigu.campusintelligentassistantsystem.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/courses")
@CrossOrigin(origins = "*")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 获取所有课程
     * @return 课程列表
     */
    @GetMapping
    public ResponseEntity<List<Course>> getAllCourses() {
        List<Course> courses = courseService.getAllCourses();
        return ResponseEntity.ok(courses);
    }

    /**
     * 根据ID获取课程
     * @param id 课程ID
     * @return 课程信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Course> getCourseById(@PathVariable Long id) {
        Course course = courseService.getCourseById(id);
        if (course != null) {
            return ResponseEntity.ok(course);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 添加新课程
     * @param course 课程信息
     * @return 保存后的课程
     */
    @PostMapping
    public ResponseEntity<Course> addCourse(@RequestBody Course course) {
        Course savedCourse = courseService.addCourse(course);
        return ResponseEntity.ok(savedCourse);
    }

    /**
     * 更新课程信息
     * @param id 课程ID
     * @param course 课程信息
     * @return 更新后的课程
     */
    @PutMapping("/{id}")
    public ResponseEntity<Course> updateCourse(@PathVariable Long id, @RequestBody Course course) {
        course.setId(id);
        Course updatedCourse = courseService.updateCourse(course);
        return ResponseEntity.ok(updatedCourse);
    }

    /**
     * 删除课程
     * @param id 课程ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteCourse(@PathVariable Long id) {
        courseService.deleteCourse(id);
        return ResponseEntity.ok("课程删除成功");
    }

    /**
     * 获取学生选课列表
     * @param studentId 学生ID
     * @return 选课列表
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<StudentCourse>> getStudentCourses(@PathVariable Long studentId) {
        List<StudentCourse> courses = courseService.getCoursesByStudentId(studentId);
        return ResponseEntity.ok(courses);
    }

    /**
     * 学生选课
     * @param studentCourse 选课信息
     * @return 选课结果
     */
    @PostMapping("/enroll")
    public ResponseEntity<StudentCourse> enrollCourse(@RequestBody StudentCourse studentCourse) {
        StudentCourse enrolled = courseService.enrollCourse(studentCourse);
        return ResponseEntity.ok(enrolled);
    }

    /**
     * 更新学生成绩
     * @param id 选课记录ID
     * @param score 成绩
     * @return 更新后的选课记录
     */
    @PutMapping("/{id}/score")
    public ResponseEntity<StudentCourse> updateScore(@PathVariable Long id, @RequestParam Double score) {
        StudentCourse updated = courseService.updateScore(id, score);
        if (updated != null) {
            return ResponseEntity.ok(updated);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}